import * as path from "path";
import * as fs from "fs";
import * as os from "os";
import * as vscode from "vscode";
import { ViewManager } from "./common/viewManager";
import { WebviewPanel, ExtensionContext } from "vscode";
// import { gContext, createProject } from "./extension";
import localize from "./localize";
import * as utils from "./utils";

export class BaseManager {
  extensionContext: ExtensionContext;

  constructor(extensionContext: ExtensionContext) {
    this.extensionContext = extensionContext;
  }
  
  /**
   * 统一处理通用的监听关键字。
   * @param viewPanel 
   * @param message 传入的消息体
   * @returns 匹配成功返回true；未匹配则返回false
   */
  public commonReceiveListener = (viewPanel: WebviewPanel, message: any) => {
    console.log("message " + message.type);
    if (message.type === "GET_HOME_DIR") {
      // 获取home路径并发送消息
      // java类型会额外返回jdk等路径
      utils.onGetPaths(this.extensionContext, viewPanel, message.projectType);
      return true;
    } else if (message.type === "SET_DIR") {
      // 保存当前目录为默认目录
      console.log("SET_DIR: ");
      console.log(message.data);
      utils.onSetDir(
        this.extensionContext,
        message.data.isSaveDir,
        message.data.projectPath
      );
      return true;
    } else if (message.type === "CHOOSE_DIR") {
      console.log("CHOOSE_DIR: ");
      console.log(message.model);
      // 选择目录，并发送消息
      utils.onChoosePrjDir(this.extensionContext, viewPanel, message.model);
      return true;
    } else if (message.type === "CALL_COMMAND") {
      if(message.command != undefined){
        vscode.commands.executeCommand(message.command, message.param)
        .then((s) => {
            if (s === null) {
                console.log("CALL_COMMAND " + message.command + " " + message.param + " successed");
            }else{
              console.log("CALL_COMMAND " + message.command + " " + message.param + " error. " + s);
            }
        });
      }
    } else if (message.type === "CALL_SETTINGS") {
      // 打开设置json 页连接
      if(message.flg === 1){
        vscode.commands.executeCommand('workbench.action.openSettingsJson', message.param)
        .then((s) => {
            if (s === null) {
                console.log("Open Settings Json successed");
            }
        });
      }else{
        // 打开设置页连接
        vscode.commands.executeCommand('workbench.action.openSettings', message.param)
        .then((s) => {
            if (s === null) {
                console.log("Open Settings successed");
            }
        });
      }
    }
    return false;
  };
}
